<?php

function online_agent_list_page()
{
    @$output = drupal_render(drupal_get_form('online_agent_list_form'));
    return $output;
}

function online_group_list_page()
{
    @$output = drupal_render(drupal_get_form('online_group_list_form'));
    return $output;
}


function online_agent_list_form($form, &$form_state)
{
//    $search_img = _is_mobile()?"":"<img src='".base_path()."sites/all/themes/tweme/images/search.png'>";
//    $res_img = _is_mobile()?"":"<img src='".base_path()."sites/all/themes/tweme/images/result.png'>";
//    $form['border_wrapper']['#markup'] =$search_img.'<div class="border_wrapper-search">';
    $form['employee_name'] = array(
        '#type' => 'textfield',
        '#prefix' =>'<div class="agent-search-form">',
        '#default_value' => '',
        '#attributes' => array('placeholder' => '客服名称'),
        '#validated' => TRUE,
    );
    $form['phone'] = array(
        '#type' => 'textfield',
        '#default_value' => '',
        '#attributes' => array('placeholder' => '电话'),
        '#maxlength' => 11,
        '#validated' => TRUE,
        '#suffix' =>'</div>',
    );
//    "</div><div class='table-div'>$res_img<div class='table-border-div'>".."</div></div>"
    $table['#markup'] = agent_list_view_page($form, $form_state);
    ajax_submit_form($form, $form_state, $table, '');
    return $form;
}

function agent_list_view_page($form, &$form_state)
{
    // if($form_state['triggering_element']['#value'] == '查询') {
        $output = '';
        $header = array(
            array('data' => '选择', 'class' => ['no']),
            array('data' => '客服名称', 'class' => ['employee_name']),
            array('data' => '客服别名', 'class' => ['alias']),
            array('data' => '省 市 县', 'class' => ['province city county']),
            array('data' => '分组', 'class' => ['group']),
            array('data' => '操作', 'class' => ['more']),
        );
        $rows = array();
        $query = db_select('online_agent', 'OA')->extend('PagerDefault')->extend('TableSort');
        $query->join('employee', 'E', 'E.employee_id=OA.uid');
        $query->join('addr','A','OA.addr_id=A.addr_id');
        $query->join('chat_groups', 'CG', 'CG.chat_group_id=OA.chat_group_id');
        $query->fields('OA');
        $query->addField('E', 'employee_name');

        if(!empty($form_state['values']['employee_name'])){
            $query->condition('E.employee_name', '%' . db_like($form_state['values']['employee_name']) . '%', 'LIKE');
        }
        if(!empty($form_state['values']['phone'])){
            $query->condition('E.phone', '%' . db_like($form_state['values']['phone']) . '%', 'LIKE');
        }
        $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
        $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
        $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
        $query->addField('A1', 'name', 'province');
        $query->addField('A2', 'name', 'city');
        $query->addField('A3', 'name', 'county');
        $query->addField('CG', 'chat_group_name');
        if($form_state['triggering_element']['#value'] != '查询')
            $query->orderBy('OA.timestamp', 'DESC');
        $query->limit(variable_get("list_page_item_number", 5));
        $result = $query->orderByHeader($header)->execute();

        $n = 1;
        foreach ($result as $row) {
            $city = $row->city ? ' '.$row->city :'';
            $county = $row->county ? ' '.$row->county :'';
            $rows[] = array(
                array('data' => '<input type="checkbox" class="listViewTableCheck" value ="' . $row->online_agent_id . '"/>' . $n++, 'class' => ['no']),
                array('data' => $row->employee_name, 'class' => ['employee_name']),
                array('data' => $row->alias, 'class' => ['employee_name']),
                array('data' => $row->province.$city.$county, 'class' => ['province city county']),
                array('data' => $row->chat_group_name, 'class' => ['chat_group_name']),
                array('data' => '<a href="'.base_path().'manage_chat/agent/edit/' . $row->online_agent_id . '" data-toggle="modal" target = "_top"><img width="20px" height="20px" src="'.base_path().'sites/all/themes/tweme/images/images/cz.png"/></a>', 'class' => 'more'),
            );
        }

        $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover'))));
        if(sizeof($rows) > 0) {
            $output .= '<a href="javascript:void(0)" id="listViewTableSelectAll" data-toggle="modal" class="btn btn-mini">全选</a>';
            $output .= '<a href="javascript:void(0)" id="listViewTableDeleteSelected" data-toggle="modal" class="btn btn-mini">删除所选</a>';
        }
        $output .= theme('pager');
        return $output;
    // }
}

function online_agent_delete()
{
    ajax_table_delete('online_agent', 'online_agent_id');
}

